/*
 * @lc app=leetcode.cn id=1640 lang=typescript
 *
 * [1640] 能否连接形成数组
 */

// @lc code=start
function canFormArray(arr: number[], pieces: number[][]): boolean {
    let keep: number[] = [];
    let cur = 0;
    for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        if (cur === keep.length) {
            const res = pieces.find((item) => {
                return item[0] === element;
            });
            if (typeof res !== "object") return false;
            keep = res;
            cur = 1;
        } else {
            if (keep[cur] !== element) return false;
            cur++;
        }
    }
    return keep.length === cur;
}
// @lc code=end
